import 'package:common_utils/common_utils.dart';
import 'package:sp_util/sp_util.dart';

class Tools {
  Tools._();

  static Tools _currentTools = Tools._();

  static Tools getInstance() {
    /// 初始化SpUtil
    SpUtil.getInstance();
    return _currentTools;
  }

  /// 比较两值大小
  /// * [ratio] “比值”标杆值|String|必填
  /// * [byRatio] “被比值”用作与比值做对比的值|String|必填
  /// * @return -1：比值小于被比值|0：两值相对|1：比值大于被比值
  static int contrast(String ratio, String byRatio) =>
      Comparable.compare(ratio, byRatio);

  /// 存储布尔类型数据
  /// * [k] String|必填|键
  /// * [v] bool|必填|值
  static void setBool(String k, bool v) => SpUtil.putBool(k, v);

  /// 获取布尔类型数据
  /// * [k] String|必填|键
  static bool getBool(String k) => SpUtil.getBool(k);

  /// 存储Map类型数据
  /// * [k] String|必填|键
  /// * [v] bool|必填|值
  static void setObj(String k, Map<dynamic, dynamic> v) =>
      SpUtil.putObject(k, v);

  /// 获取Map类型数据
  /// * [k] String|必填|键
  static Map<dynamic, dynamic> getObj(String k) => SpUtil.getObject(k);

  /// 存储字符串类型数据
  /// * [k] String|必填|键
  /// * [v] bool|必填|值
  static void setString(String k, String v) => SpUtil.putString(k, v);

  /// 获取字符串类型数据
  /// * [k] String|必填|键
  static String getString(String k) => SpUtil.getString(k);

  /// 存储数组对象
  /// * [k] String|必填|键
  /// * [v] bool|必填|值
  static void setObjectList(String k, List<Object> v) =>
      SpUtil.putObjectList(k, v);

  /// 获取数组对象
  /// * [k] String|必填|键
  static List<Map> getObjectList(String k) => SpUtil.getObjectList(k);

  /// 获取数组对象
  /// * [k] String|必填|键
  /// * [callback] Function|必填|回调|注：提供map类型描述
  static List getObjList(String k, Function callback) =>
      SpUtil.getObjList(k, (e) => callback(e));

  /// 保留指定项其他全部删除
  /// * [k] String|必填|需要保留的键名
  static void specifyReservedContent(String k) {
    Set<String> _map = SpUtil.getKeys();
    _map.retainWhere((it) {
      if (it != k) remove(it);
      return false;
    });
  }

  /// 移除指定的存储内容
  /// * [k] String|必填|键
  static void remove(String k) => SpUtil.remove(k);

  /// 移除所以存储对象
  static void clear() => SpUtil.clear();

  /// 获取日期格式
  /// * [t] DateTime|日期|必填
  /// * [f] String|格式|必填
  static String dateFormat(DateTime t, String f) =>
      DateUtil.formatDate(t, format: f);
}

class DataFormats {
  static String full = "yyyy-MM-dd HH:mm:ss";
  static String y_mo_d_h_m = "yyyy-MM-dd HH:mm";
  static String y_mo_d = "yyyy-MM-dd";
  static String y_mo = "yyyy-MM";
  static String mo_d = "MM-dd";
  static String mo_d_h_m = "MM-dd HH:mm";
  static String h_m_s = "HH:mm:ss";
  static String h_m = "HH:mm";

  static String zh_full = "yyyy年MM月dd日 HH时mm分ss秒";
  static String zh_y_mo_d_h_m = "yyyy年MM月dd日 HH时mm分";
  static String zh_y_mo_d = "yyyy年MM月dd日";
  static String zh_y_mo = "yyyy年MM月";
  static String zh_mo_d = "MM月dd日";
  static String zh_mo_d_h_m = "MM月dd日 HH时mm分";
  static String zh_h_m_s = "HH时mm分ss秒";
  static String zh_h_m = "HH时mm分";
}
